package com.demo.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridView;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ui.RootPanel;
import com.streamhub.client.StreamHubGWTAdapter;
import com.streamhub.client.StreamHubGWTUpdateListener;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ExtGwtApp implements EntryPoint {

	private static final String[] STOCKS = { "AA","CAT","DIS","GE","HD","IBM","INTC","JPM","MCD","MMM","MSFT","WMT" };

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final StreamHubGWTAdapter hub = new StreamHubGWTAdapter();
		hub.connect("http://localhost:7979/streamhub/");
		final NumberFormat formatter = NumberFormat.getFormat("0.00");

		GridCellRenderer<Stock> change = new GridCellRenderer<Stock>() {
			public String render(Stock model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<Stock> store, Grid<Stock> grid) {
				double val = (Double) model.get(property);
				String style = val < 0 ? "red" : "green";
				return "<span style='color:" + style + "'>" + formatter.format(val) + "</span>";
			}
		};

		GridCellRenderer<Stock> percentChange = new GridCellRenderer<Stock>() {
			public String render(Stock model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<Stock> store, Grid<Stock> grid) {
				double val = (Double) model.get(property);
				String style = val < 0 ? "red" : "green";
				return "<span style='color:" + style + "'>" + formatter.format(val) + "%</span>";
			}
		};

		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId("Company");
		column.setHeader("Company");
		column.setWidth(200);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("Symbol");
		column.setHeader("Symbol");
		column.setWidth(100);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("LastPrice");
		column.setHeader("Last");
		column.setAlignment(HorizontalAlignment.RIGHT);
		column.setWidth(75);
		column.setNumberFormat(formatter);
		configs.add(column);

		column = new ColumnConfig("Change", "Change", 100);
		column.setAlignment(HorizontalAlignment.RIGHT);
		column.setRenderer(change);
		configs.add(column);

		column = new ColumnConfig("PercentChange", "% Change", 100);
		column.setAlignment(HorizontalAlignment.RIGHT);
		column.setRenderer(percentChange);
		configs.add(column);

		final ListStore<Stock> store = new ListStore<Stock>();
		final Map<String, Stock> stocks = new HashMap<String, Stock>();
		
		for (String symbol : STOCKS) {
			Stock stock = new Stock(symbol);
			stocks.put(symbol, stock);
			store.add(stock);
			hub.subscribe(symbol, new StreamHubGWTUpdateListener() {
				public void onUpdate(String topic, JSONObject update) {
					String company = ((JSONString) update.get("Company")).stringValue();
					String lastPrice = ((JSONString) update.get("LastPrice")).stringValue();
					String change = ((JSONString) update.get("Change")).stringValue();
					String percentChange = ((JSONString) update.get("PercentChange")).stringValue();
					Stock stock = stocks.get(topic);
					stock.set("Company", company);
					stock.set("LastPrice", Double.parseDouble(lastPrice));
					stock.set("Change", Double.parseDouble(change));
					stock.set("PercentChange", Double.parseDouble(percentChange.replace("%", "")));
					store.update(stock);
				}
			});
		}

		ColumnModel cm = new ColumnModel(configs);

		ContentPanel cp = new ContentPanel();
		cp.setBodyBorder(false);
		cp.setHeading("Ext GWT Grid");
		cp.setButtonAlign(HorizontalAlignment.CENTER);
		cp.setLayout(new FitLayout());
		cp.setSize(600, 200);

		Grid<Stock> grid = new Grid<Stock>(store, cm);
		grid.setStyleAttribute("borderTop", "none");
		grid.setBorders(true);
		grid.setStripeRows(true);
		grid.setView(new NoRowFocusGridView());
		cp.add(grid);

		RootPanel rootPanel = RootPanel.get("gridContainer");
		rootPanel.add(cp);
	}
	
	private class NoRowFocusGridView extends GridView {
		@Override
		public void focusRow(int rowIndex) {
		
		}
	}
}
